#!/usr/bin/env bash

#define icons for workspaces 1-9
ic=(0 "" "" "" "" "" "")

workspaces() {

	unset -v \
		o1 o2 o3 o4 o5 o6 \
		f1 f2 f3 f4 f5 f6

	# check if Occupied
	for num in $(hyprctl workspaces | grep ID | awk '{print $3}'); do
		export o"$num"="$num"
	done

	#check if Focused
	for num in $(hyprctl monitors | awk 'NR==3 {print $3}'); do
		export f"$num"="$num"
	done

	echo -n "(box :class \"works\" \
	              :orientation \"h\" \
	              :spacing 15 \
	              :space-evenly \"true\" \
	              "
	for i in {1..6}; do
		eval "echo -n \"(button :onclick 'hyprctl dispatch workspace $i'  \
	                           :class 'w\$o$i\$f$i' '\${ic[$i]}') \
	                           \" "
	done
	echo ")"
}

workspaces

tail -f /tmp/hypr/"$HYPRLAND_INSTANCE_SIGNATURE"/hyprland.log | grep --line-buffered "Changed to workspace\|Destroying workspace" | while read -r; do
	workspaces
done
